package cn.yang.domain.activity.service.quota;

import cn.yang.domain.activity.model.aggregate.CreateQuotaOrderAggregate;
import cn.yang.domain.activity.model.entity.*;
import cn.yang.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import cn.yang.domain.activity.model.valobj.OrderStateVO;
import cn.yang.domain.activity.repository.IActivityRepository;
import cn.yang.domain.activity.service.IRaffleActivitySkuStockService;
import cn.yang.domain.activity.service.quota.policy.ITradePolicy;
import cn.yang.domain.activity.service.quota.rule.factory.DefaultActivityChainFactory;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @version v1.0
 * @ClassName: cn.yang.domain.activity.service
 * @Description: 抽奖活动
 * @Author: YJ
 */
@Service
public class RaffleActivityAccountQuotaService extends AbstractRaffleActivityAccountQuotaService implements IRaffleActivitySkuStockService {
    public RaffleActivityAccountQuotaService(DefaultActivityChainFactory defaultActivityChainFactory,
                                             IActivityRepository activityRepository,
                                             Map<String, ITradePolicy> tradePolicyGroup
                                             ) {
        super(defaultActivityChainFactory, activityRepository,tradePolicyGroup);
    }

    /**
     * 存储订单
     *
     * @param createQuotaOrderAggregate 聚合对象
     */


    @Override
    protected CreateQuotaOrderAggregate buildOrderAAggregate(SkuRechargeEntity skuRechargeEntity, ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        //订单实体对象
        ActivityOrderEntity activityOrderEntity = new ActivityOrderEntity();
        activityOrderEntity.setUserId(skuRechargeEntity.getUserId());
        activityOrderEntity.setSku(skuRechargeEntity.getSku());
        activityOrderEntity.setActivityId(activityEntity.getActivityId());
        activityOrderEntity.setActivityName(activityEntity.getActivityName());
        activityOrderEntity.setStrategyId(activityEntity.getStrategyId());
        //公司里一般会有专门的雪花算法UUID服务，我们这里直接生成一个12位就可以了
        activityOrderEntity.setOrderId(RandomStringUtils.randomNumeric(12));
        activityOrderEntity.setOrderTime(new Date());
        activityOrderEntity.setTotalCount(activityCountEntity.getTotalCount());
        activityOrderEntity.setDayCount(activityCountEntity.getDayCount());
        activityOrderEntity.setMonthCount(activityCountEntity.getMonthCount());
        activityOrderEntity.setState(OrderStateVO.completed);
        activityOrderEntity.setPayAmount(activitySkuEntity.getProductAmount());
        activityOrderEntity.setOutBusinessNo(skuRechargeEntity.getOutBusinessNo());

        return CreateQuotaOrderAggregate.builder()
                .userId(skuRechargeEntity.getUserId())
                .activityId(activityEntity.getActivityId())
                .totalCount(activityCountEntity.getTotalCount())
                .dayCount(activityCountEntity.getDayCount())
                .monthCount(activityCountEntity.getMonthCount())
                .activityOrderEntity(activityOrderEntity)
                .build();
    }

    @Override
    public ActivitySkuStockKeyVO takeQueueValue() throws InterruptedException {
        return activityRepository.takeQueueValue();
    }

    @Override
    public void clearQueueValue() {
        activityRepository.clearQueueValue();
    }

    @Override
    public void updateActivitySkuStock(Long sku) {
        activityRepository.updateActivitySkuStock(sku);
    }

    @Override
    public void clearActivitySkuStock(Long sku) {
        activityRepository.clearActivitySkuStock(sku);
    }

    @Override
    public void updateOrder(DeliveryOrderEntity deliveryOrderEntity) {
        activityRepository.updateOrder(deliveryOrderEntity);
    }

    @Override
    public Integer queryRaffleActivityAccountDayPartakeCount(String userId, Long activityId) {
        return activityRepository.queryRaffleActivityAccountDayPartakeCount(userId, activityId);
    }

    @Override
    public ActivityAccountEntity queryRaffleActivityEntity(String userId, Long activityId) {
        return activityRepository.queryRaffleActivityEntity(userId,activityId);
    }

    @Override
    public Integer queryRaffleActivityAccountPartakeCount(String userId, Long activityId) {
        return activityRepository.queryRaffleActivityAccountPartakeCount(userId, activityId);
    }
}
